package online.foxnull2.foxmall.coupon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import online.foxnull2.foxmall.coupon.entity.SeckillSessionEntity;
import online.foxnull2.foxmall.coupon.mapper.SeckillSessionMapper;
import online.foxnull2.foxmall.coupon.service.SeckillSessionService;
import online.foxnull2.foxmall.coupon.utils.PageUtils;
import online.foxnull2.foxmall.coupon.utils.Query;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

@Slf4j
@Service("seckillSessionService")
public class SeckillSessionServiceImpl extends ServiceImpl<SeckillSessionMapper, SeckillSessionEntity> implements SeckillSessionService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SeckillSessionEntity> page = this.page(
                new Query<SeckillSessionEntity>().getPage(params),
                new QueryWrapper<>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<SeckillSessionEntity> listSeckillSessionWithNextThreeDays() {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String val1 = LocalDateTime.of(LocalDate.now(), LocalTime.MIN).format(dateTimeFormatter);
        String val2 = LocalDateTime.of(LocalDate.now().plusDays(2L), LocalTime.MAX).format(dateTimeFormatter);
        List<SeckillSessionEntity> seckillSessionEntities = baseMapper.selectList(new QueryWrapper<SeckillSessionEntity>().between("start_time", val1, val2));
        log.debug("listSeckillSessionWithNextThreeDays -> {}", seckillSessionEntities);
        return seckillSessionEntities;
    }

}